package com.ll.controller;

import com.ll.domain.Account;
import com.ll.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 账户web
 * 2020/9/14 - 11:26
 */
@Controller
@RequestMapping("/account")
public class AccountController {

    @Autowired
    private AccountService accountService;

    //查询所有
    @RequestMapping("/findAll")
    public String findAll(Model model){
        System.out.println("AccountController => findAll()");
        System.out.println("表现层: 查询所有账户...");

        //调用service
        List<Account> list = accountService.findAll();

        model.addAttribute("list", list);

        return "list";
    }

    /**
     *
     * @return
     */
    @RequestMapping("/save")
    public void save(Account account,HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("AccountController => save()");
        System.out.println("表现层: 保存账户...");

        //调用service
        accountService.saveAccount(account);

        System.out.println("请求内容路径: " + request.getContextPath());

        response.sendRedirect(request.getContextPath() + "/account/findAll");

        return;
    }

}
